package com.hmall.trade.Listener;

import com.hmall.api.client.PayClient;
import com.hmall.api.dto.PayOrderDTO;
import com.hmall.common.domain.MultiDelayMessage;
import com.hmall.trade.constants.MQConstants;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.mq.DelayMessageProcessor;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * CLassName: OrderDelayMessageListener
 * Description:
 * Author: wuzhixiang
 * Date: 2024-11-24 22:06
 * Version: 1.0
 */
@Component
@RequiredArgsConstructor
public class OrderStatusCheckListener {

    private final IOrderService orderService;

    private final PayClient payClient;

    private final RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.DELAY_ORDER_QUEUE_NAME,durable = "true"),
            exchange = @Exchange(name = MQConstants.DELAY_EXCHANGE_NAME, delayed = "true",type = ExchangeTypes.TOPIC),
            key = MQConstants.DELAY_ORDER_KEY
    ))
    public void listenOrderDelayMessage(MultiDelayMessage<Long> msg) {
        // 1.获取消息中的订单id
        Long orderId = msg.getData();
        //1 查询订单，判断状态(查本地的订单)
        Order order = orderService.getById(orderId);
        //2 查询订单状态,判断是否已支付
        if (null == order || order.getStatus() != 1) {
            return;
        }
        //3 未支付，需要查询支付流水状态(可能是MQ消息没有通知过来或者中断)
        PayOrderDTO payOrderDTO = payClient.queryPayOrderByBizOrderNo(orderId);
        //4 判断是否支付
        if (payOrderDTO != null && payOrderDTO.getStatus() == 3) {
            //4.1 已支付，标记订单状态为已支付
            orderService.markOrderPaySuccess(orderId);
            return;
        }
        // 4.确定未支付，判断是否还有剩余延迟时间
        if (msg.hasNextDelay()) {
            // 4.1.有延迟时间，需要重发延迟消息，先获取延迟时间的int值
            int nextDelay = msg.removeNextDelay().intValue();
            // 4.2.发送延迟消息
            rabbitTemplate.convertAndSend(MQConstants.DELAY_EXCHANGE_NAME,
                    MQConstants.DELAY_ORDER_KEY,
                    msg,
                    new DelayMessageProcessor(nextDelay));
            return;
        }
        // 5.没有剩余延迟时间了，说明订单超时未支付，需要取消订单
        orderService.cancelOrder(orderId);
    }
    /*public void listenOrderDelayMessage(Long orderId){
        //1 查询订单
        Order order = orderService.getById(orderId);
        //2 查询订单状态,判断是否已支付
        if(order == null || order.getStatus() != 1){
            return;
        }
        //3 未支付，需要查询支付流水状态
        PayOrderDTO payOrderDTO = payClient.queryPayOrderByBizOrderNo(orderId);
        //4 判断是否支付
        if(payOrderDTO != null || payOrderDTO.getStatus() == 3){
            //4.1 已支付，标记订单状态为已支付
            orderService.markOrderPaySuccess(orderId);
        }else {
            //4.2 未支付，取消订单，恢复库存
            orderService.cancelOrder(orderId);
        }
    }*/
}
